package com.andaily.infrastructure.support.cloudfoundary;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @author Shengzhao Li
 */
public abstract class CloudFoundryUtils {

    public static final String MYSQL_LABEL = "mysql-5.1";

    /**
     * When we use CloudFoundry Mysql Service,
     * call <code>java.lang.System.getenv("VCAP_SERVICES")</code>  get the current Mysql information as text.
     * <p/>
     * The method Use JSON lib parse Mysql information to object {@link CloudFoundryDataSourceDetails} array
     * if you need.
     * <p/>
     * More information see <i>http://docs.cloudfoundry.com/services/mysql/mysql.html</i>.
     *
     * @param originalDbText The Mysql original json data as text
     * @return CloudFoundryDataSourceDetails array
     */
    public static CloudFoundryDataSourceDetails[] parseMySqlDataSourceDetails(String originalDbText) {
        JSONObject jsonObject = JSONObject.fromObject(originalDbText);
        JSONArray jsonArray = jsonObject.getJSONArray(MYSQL_LABEL);

        JsonConfig jsonConfig = new JsonConfig();
        return (CloudFoundryDataSourceDetails[]) JSONArray.toArray(jsonArray, new CloudFoundryDataSourceDetails(), jsonConfig);
    }
}
